import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  int pretime = 0;
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async{
        int now = DateTime.now().microsecond;
        int flag = now - pretime;
        if(flag > 1000){
          pretime = now;
          return false;
        }
        showTips();
        // 拦截
        return false;
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text("WillPopScope 双击返回与界面退出提示"),
        ),
        body: Container(
          padding: EdgeInsets.all(30),
          child: Column(
            children: [
              Text("在安卓中点击物理返回按钮，或者全面屏的手势退出，或者ios中的左滑退出，在Fultter中可以使用willPopScope拦截退出"),
            ],
          ),
        ),
      ),
    );
    
  }
  void showTips(){
    showDialog(context: context, builder: (BuildContext context){
      return new AlertDialog(
        title: Text("提示"),
        content: Text("您确定要退出吗?"),
        actions: [
          TextButton(onPressed: (){}, child: Text("确定")),
          TextButton(onPressed: (){
            Navigator.of(context).pop();
          }, child: Text("取消"))
        ],
      );
    });
  }
}